package com.chenyi.shop.service.impl;

import java.util.List;

import com.chenyi.common.core.redis.RedisCache;
import com.chenyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.chenyi.shop.mapper.SysShopMapper;
import com.chenyi.common.core.domain.entity.SysShop;
import com.chenyi.shop.service.ISysShopService;

import javax.annotation.PostConstruct;

/**
 * 商户管理Service业务层处理
 * 
 * @author chenyi
 * @date 2020-10-22
 */
@Service
public class SysShopServiceImpl implements ISysShopService 
{
    @Autowired
    private SysShopMapper sysShopMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 项目启动时，初始化参数到缓存
     */
    @PostConstruct
    public void init()
    {
        String shopNo = sysShopMapper.selectFinallyShopNo();
        redisCache.setCacheObject("sys_no:shop_no",shopNo);
    }

    /**
     * 查询商户管理
     * 
     * @param shopId 商户管理ID
     * @return 商户管理
     */
    @Override
    public SysShop selectSysShopById(Long shopId)
    {
        return sysShopMapper.selectSysShopById(shopId);
    }

    /**
     * 查询商户管理列表
     * 
     * @param sysShop 商户管理
     * @return 商户管理
     */
    @Override
    public List<SysShop> selectSysShopList(SysShop sysShop)
    {
        return sysShopMapper.selectSysShopList(sysShop);
    }

    /**
     * 新增商户管理
     * 
     * @param sysShop 商户管理
     * @return 结果
     */
    @Override
    public int insertSysShop(SysShop sysShop)
    {
        sysShop.setCreateTime(DateUtils.getNowDate());
        sysShop.setShopNo(getShopNo());
        return sysShopMapper.insertSysShop(sysShop);
    }

    /**
     * 修改商户管理
     * 
     * @param sysShop 商户管理
     * @return 结果
     */
    @Override
    public int updateSysShop(SysShop sysShop)
    {
        sysShop.setUpdateTime(DateUtils.getNowDate());
        return sysShopMapper.updateSysShop(sysShop);
    }

    /**
     * 批量删除商户管理
     * 
     * @param shopIds 需要删除的商户管理ID
     * @return 结果
     */
    @Override
    public int deleteSysShopByIds(Long[] shopIds)
    {
        return sysShopMapper.deleteSysShopByIds(shopIds);
    }

    /**
     * 删除商户管理信息
     * 
     * @param shopId 商户管理ID
     * @return 结果
     */
    @Override
    public int deleteSysShopById(Long shopId)
    {
        return sysShopMapper.deleteSysShopById(shopId);
    }

    @Override
    public SysShop selectShopByUserName(String userName) {
        return sysShopMapper.selectShopByUserName(userName);
    }

    @Override
    public SysShop selectShopByToken(String token) {
        return sysShopMapper.selectShopByToken(token);
    }

    @Override
    public int updateShopProfile(SysShop shop) {
        return sysShopMapper.updateSysShop(shop);
    }

    @Override
    public int resetShopPwd(String userName, String password) {
        return sysShopMapper.resetShopPwd(userName, password);
    }

    @Override
    public boolean updateShopAvatar(String userName, String avatar) {
        return sysShopMapper.updateShopAvatar(userName, avatar) > 0;
    }

    /* 功能描述: <br>
     * 〈获取商户编号〉
     *
     * @param
     * @return: java.lang.String
     * @Author: wangkh
     * @Date: 2020/11/2 0002 15:29
     */
    private String getShopNo(){
        String sysNo = redisCache.getCacheObject("sys_no:shop_no");
        String prefix = sysNo.substring(0,2);
        Integer shopNo = Integer.parseInt(sysNo.substring(2)) + 1;
        return prefix + shopNo;
    }
}
